<?php
/**
 * 
 * @author Youssef Driouach
 *
 */
class Application_Model_ComputerMapper{

	protected $_dbTable;

	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_Computer');
		}
		return $this->_dbTable;
	}
	
	public function save(Application_Model_Computer $computer){
		
		
		$productMapper = new Application_Model_ProductMapper();
		$row = $productMapper->save($computer);
		//TODO set the object save function to return the id of the object saved
		//$where = array('model = ?' => (string)$product->getModel());
		$computer->setProductId($row);
		
		$data = array(
				'id_product' 	=> $computer->getProductId(),
				'processor'		=> $computer->getProcessor(),
				'ram'			=> $computer->getRam(),
				'hdd'			=> $computer->getHdd(),
				'videocard'		=> $computer->getVideocard(),
				'os'			=> $computer->getOs(),
		);
		
		if (null === ($id = $computer->getcomputerId())) {
			unset($data['id_computer']);
			return $this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id_computer = ?' => $id));
			return $id;
		}
	}
	
	public function find($id, Application_Model_Computer $computer)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$computer->setComputerId($row->id_computer);
		$computer->setProductId($row->id_product);
		$computer->setProcessor($row->processor);
		$computer->setRam($row->ram);
		$computer->setHdd($row->hdd);
		$computer->setVideocard($row->videocard);
		$computer->setOs($row->os);
		
		$productMapper = new Application_Model_ProductMapper();
		$computer = $productMapper->find($computer->getProductId(), $computer);
	
		return $computer;
	}
	
	public function delete($id)
	{
	
		$computer= new Application_Model_Computer();
		$productMapper = new Application_Model_ProductMapper();
		$computer = $this->find($id, new Application_Model_Computer());
	
		$result = $this->getDbTable()->find($id);
		if (0 != count($result)) {
			$this->getDbTable()->delete(array('id_computer = ?' => $id));
		}
	
		$productMapper->delete($computer->getProductId());
	
	
	}
	
	public function indexBuilder(Zend_Search_Lucene_Document $doc, Application_Model_Computer $computer)
	{
		$productMapper = new Application_Model_ProductMapper();
		$requestBuilder = new Application_Model_Builder_Request();
		$doc->addField(Zend_Search_Lucene_Field::Text('processor', $computer->getProcessor(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('ram',$computer->getRam(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('hdd',$computer->getHdd(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('videocard',$computer->getVideocard(), 'utf-8'));
		$doc->addField(Zend_Search_Lucene_Field::Text('os',$computer->getOs(), 'utf-8'));
		$doc = $productMapper->indexBuilder($doc, $computer);
	
		return $doc;
	}
	
}